import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { RoutingStepModel } from '#/api/process/models';

import { getOperationListApi } from '#/api/process/operation';

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'ApiSelect',
      componentProps: {
        showSearch: true,
        optionFilterProp: 'label',
        labelField: 'opName',
        valueField: 'id',
        api: getOperationListApi,
        immediate: false,
      },
      fieldName: 'operationId',
      label: '工序',
      rules: 'required',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
      },
      fieldName: 'isCritical',
      label: '关键工序',
      rules: 'required',
      defaultValue: true,
      help: '关键工序必须要过站，进站时会检查',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
      },
      fieldName: 'isMustGood',
      label: '良品过站',
      rules: 'required',
      defaultValue: true,
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
      },
      fieldName: 'allowRepeat',
      label: '允许重复进站',
      rules: 'required',
      defaultValue: false,
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
      },
      fieldName: 'isCheckMaterial',
      label: '检查物料',
      rules: 'required',
      defaultValue: true,
      help: '检查关键物料是否有上齐，出站时会校验',
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 0,
      },
      fieldName: 'standardTime',
      label: '作业时长(s)',
      defaultValue: 0,
    },
    {
      component: 'Select',
      componentProps: {
        mode: 'multiple',
      },
      fieldName: 'policies',
      label: '策略',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        optionType: 'button',
        options: [
          { label: '否', value: false },
          { label: '是', value: true },
        ],
      },
      fieldName: 'isCheckStandard',
      label: '校验工艺标准',
      defaultValue: false,
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'workCenter',
      label: '工作中心',
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 255,
      },
      fieldName: 'stepInstruction',
      label: '步骤说明',
      formItemClass: 'col-span-2 items-baseline',
    },
  ];
}

/**
 * 获取表格列配置。
 */
export function useColumns(): VxeTableGridOptions<RoutingStepModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'operation.opName', title: '工序', width: 120 },
    {
      field: 'sort',
      title: '排序',
      width: 90,
      slots: { default: 'sort' },
    },
    {
      field: 'isCritical',
      title: '关键工序',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'blue', label: '是', value: true },
          { color: 'yellow', label: '否', value: false },
        ],
      },
    },
    {
      field: 'isMustGood',
      title: '必须良品过站',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'cyan', label: '是', value: true },
          { color: 'red', label: '否', value: false },
        ],
      },
    },
    {
      field: 'allowRepeat',
      title: '允许重复进站',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'orange', label: '是', value: true },
          { color: 'blue', label: '否', value: false },
        ],
      },
    },
    {
      field: 'isCheckMaterial',
      title: '是否检查物料',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'cyan', label: '是', value: true },
          { color: 'red', label: '否', value: false },
        ],
      },
    },
    { field: 'policies', title: '策略', width: 120 },
    { field: 'standardTime', title: '标准作业时间（秒）', width: 140 },
    {
      field: 'isCheckStandard',
      title: '校验工艺标准',
      width: 110,
      formatter: ({ cellValue }) => (cellValue ? '是' : '否'),
    },
    { field: 'createdTime', title: '创建时间', minWidth: 160 },
    {
      field: 'action',
      title: '操作',
      fixed: 'right',
      width: 160,
      slots: { default: 'action' },
    },
  ];
}
